import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.*;
import java.util.List;

public class StudentRegisterFrame extends JFrame {
    private JTextField classField = new JTextField(10);
    private JTextField gradeField = new JTextField(10);
    private JTextField majorField = new JTextField(10);
    private JTextField collegeField = new JTextField(10);
    private JTextField studentIdField = new JTextField(10);

    private JRadioButton participantBtn = new JRadioButton("参赛项目");
    private JRadioButton volunteerBtn = new JRadioButton("志愿者");
    private JRadioButton refereeBtn = new JRadioButton("裁判员");

    private JTable projectTable;
    private DefaultTableModel tableModel;

    private JButton registerBtn = new JButton("报名");

    private EventManager eventManager;

    public StudentRegisterFrame(EventManager eventManager) {
        this.eventManager = eventManager;

        setTitle("河南工业大学学生报名");
        setSize(600, 550);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        Color pink = new Color(255, 192, 203);
        getContentPane().setBackground(pink);

        JPanel formPanel = new JPanel(new GridBagLayout());
        formPanel.setBackground(pink);
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(10, 10, 10, 10);
        gbc.anchor = GridBagConstraints.EAST;

        // 信息输入行
        addFormRow(formPanel, gbc, 0, "班级:", classField);
        addFormRow(formPanel, gbc, 1, "年级:", gradeField);
        addFormRow(formPanel, gbc, 2, "专业:", majorField);
        addFormRow(formPanel, gbc, 3, "学院:", collegeField);
        addFormRow(formPanel, gbc, 4, "学号:", studentIdField);

        // 报名类型
        JPanel typePanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 0));
        typePanel.setBackground(pink);
        participantBtn.setSelected(true);
        ButtonGroup group = new ButtonGroup();
        group.add(participantBtn);
        group.add(volunteerBtn);
        group.add(refereeBtn);
        typePanel.add(new JLabel("报名类型: "));
        typePanel.add(participantBtn);
        typePanel.add(volunteerBtn);
        typePanel.add(refereeBtn);

        gbc.gridx = 0; gbc.gridy = 5; gbc.gridwidth = 2;
        gbc.anchor = GridBagConstraints.CENTER;
        formPanel.add(typePanel, gbc);

        // 项目表格
        String[] columns = {"项目ID", "项目名称", "地点"};
        tableModel = new DefaultTableModel(columns, 0);
        projectTable = new JTable(tableModel);
        projectTable.setRowHeight(25);
        JScrollPane tableScroll = new JScrollPane(projectTable);
        loadProjects();

        // 报名按钮
        registerBtn.setBackground(new Color(255, 182, 193));
        JPanel btnPanel = new JPanel();
        btnPanel.setBackground(pink);
        btnPanel.add(registerBtn);

        // 布局
        setLayout(new BorderLayout(10, 10));
        add(formPanel, BorderLayout.NORTH);
        add(tableScroll, BorderLayout.CENTER);
        add(btnPanel, BorderLayout.SOUTH);

        // 事件
        registerBtn.addActionListener(e -> register());
    }

    private void addFormRow(JPanel panel, GridBagConstraints gbc, int row, String label, JTextField field) {
        JLabel lbl = new JLabel(label, SwingConstants.RIGHT);
        lbl.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        gbc.gridx = 0; gbc.gridy = row; gbc.anchor = GridBagConstraints.EAST;
        panel.add(lbl, gbc);
        gbc.gridx = 1; gbc.anchor = GridBagConstraints.WEST;
        field.setPreferredSize(new Dimension(200, 25));
        panel.add(field, gbc);
    }

    private void loadProjects() {
        List<Event> events = eventManager.getAllEvents();
        for (Event e : events) {
            tableModel.addRow(new Object[]{e.getId(), e.getName(), e.getLocation()});
        }
    }

    private void register() {
        String clazz = classField.getText().trim();
        String grade = gradeField.getText().trim();
        String major = majorField.getText().trim();
        String college = collegeField.getText().trim();
        String studentId = studentIdField.getText().trim();

        if (clazz.isEmpty() || grade.isEmpty() || major.isEmpty() || college.isEmpty() || studentId.isEmpty()) {
            JOptionPane.showMessageDialog(this, "请填写完整信息！");
            return;
        }

        int selectedRow = projectTable.getSelectedRow();
        if (selectedRow == -1) {
            JOptionPane.showMessageDialog(this, "请选择一个项目！");
            return;
        }

        String type = participantBtn.isSelected() ? "参赛项目" : (volunteerBtn.isSelected() ? "志愿者" : "裁判员");
        String projectId = (String) tableModel.getValueAt(selectedRow, 0);
        String projectName = (String) tableModel.getValueAt(selectedRow, 1);

        JOptionPane.showMessageDialog(this,
                String.format("报名成功！\n学号: %s\n类型: %s\n项目: %s", studentId, type, projectName));
        // TODO: 保存报名信息（持久化）
    }

    public static void main(String[] args) {
        EventManager em = new EventManager();
        em.addEvent(new Manager("M001", "管理员"), new Event("E001", "100米短跑", "规则", new java.util.Date(), new java.util.Date(), "田径场"));
        em.addEvent(new Manager("M001", "管理员"), new Event("E002", "跳远", "规则", new java.util.Date(), new java.util.Date(), "田径场"));

        SwingUtilities.invokeLater(() -> {
            new StudentRegisterFrame(em).setVisible(true);
        });
    }
}
